//
//  ICRedPacketInputView.m
//  ICome
//
//  Created by 席鹏飞 on 2016/11/8.
//  Copyright © 2016年 iCom. All rights reserved.
//

#import "ICRedPacketInputView.h"
#import "NSString+YYAdd.h"
#import "ICHeaderFile.h"

#define MaxTextNum 20

@implementation ICRedPacketInputView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.layer.borderWidth = 0.5;
        self.layer.borderColor = LINEBACKGROUNDCOLOR.CGColor;
        self.layer.cornerRadius = 5.0;
    }
    return self;
}

- (void)setContentDict:(NSDictionary *)contentDict {
    self.leftLabel.text = contentDict[@"left"];
    self.rightLabel.text = contentDict[@"right"];
    [self.inputTextField setValue:[UIFont systemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];
    self.inputTextView.placeholderFont = [UIFont systemFontOfSize:16];
    CGFloat leftWidth = [self.leftLabel.text sizeForFont:[UIFont systemFontOfSize:16] size:CGSizeMake(MAXFLOAT,MAXFLOAT) mode:NSLineBreakByWordWrapping].width;
    CGFloat rightWidth = [self.rightLabel.text sizeForFont:[UIFont systemFontOfSize:16] size:CGSizeMake(MAXFLOAT,MAXFLOAT) mode:NSLineBreakByWordWrapping].width;
    CGFloat textViewHeight = [self.inputTextView.text sizeForFont:[UIFont systemFontOfSize:16] size:CGSizeMake(MAXFLOAT,MAXFLOAT) mode:NSLineBreakByWordWrapping].height;
    CGFloat rightOffset = self.rightLabel.text.length > 0 ? -11 : 0;
    self.inputTextViewWidth = App_RootCtr.view.width - (15*2 + 11 + leftWidth+1 +1 + 20 + 6);
    
    [self.leftLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(11);
        make.top.equalTo(self.mas_top);
        make.bottom.equalTo(self.mas_bottom);
        make.width.mas_equalTo(leftWidth+1);
    }];
    [self.rightLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right).offset(rightOffset);
        make.top.equalTo(self.mas_top);
        make.bottom.equalTo(self.mas_bottom);
        make.width.mas_equalTo(rightWidth+1);
    }];
    if ([self.leftLabel.text isEqualToString:@"留言"]) {
        [self.inputTextView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.mas_centerY).offset(2);
            make.left.equalTo(self.leftLabel.mas_right).offset(20);
            make.right.equalTo(self.rightLabel.mas_left).offset(-6);
            make.height.mas_equalTo(textViewHeight*2);
        }];
    }else {
        [self.inputTextField mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.leftLabel.mas_right).offset(20);
            make.top.equalTo(self.mas_top);
            make.right.equalTo(self.rightLabel.mas_left).offset(-13);
            make.bottom.equalTo(self.mas_bottom);
        }];
    }
}

#pragma mark - ICTextViewDelegate

- (void)textViewTextDidChange:(ICTextView *)textView {
    /** 限制UITextView的字数 */
    if (textView.text.length > MaxTextNum)
    {
        textView.text = [textView.text substringToIndex:MaxTextNum];
    }
}

#pragma mark - Getter

- (UILabel *)leftLabel {
    if (!_leftLabel) {
        _leftLabel = [[UILabel alloc] init];
        _leftLabel.textColor = ICRGB(0x000000);
        _leftLabel.font = [UIFont systemFontOfSize:16];
        [self addSubview:_leftLabel];
    }
    return _leftLabel;
}

- (UITextField *)inputTextField {
    if (!_inputTextField) {
        _inputTextField = [[UITextField alloc] initWithFrame:CGRectZero];
        _inputTextField.backgroundColor = [UIColor clearColor];
        _inputTextField.textAlignment = NSTextAlignmentRight;
        _inputTextField.font = [UIFont systemFontOfSize:16];
        [self addSubview:_inputTextField];
    }
    return _inputTextField;
}

- (ICTextView *)inputTextView {
    if (!_inputTextView) {
        _inputTextView = [[ICTextView alloc] initWithFrame:CGRectZero];
        _inputTextView.textContainerInset = UIEdgeInsetsMake(7, 0, 0, 0);
        _inputTextView.backgroundColor = [UIColor clearColor];
        _inputTextView.textColor = [UIColor blackColor];
        _inputTextView.font = [UIFont systemFontOfSize:16];
        _inputTextView.tvDelegate = self;
        [self addSubview:_inputTextView];
    }
    return _inputTextView;
}

- (UILabel *)rightLabel {
    if (!_rightLabel) {
        _rightLabel = [[UILabel alloc] init];
        _rightLabel.textColor = ICRGB(0x000000);
        _rightLabel.font = [UIFont systemFontOfSize:16];
        [self addSubview:_rightLabel];
    }
    return _rightLabel;
}

@end
